package com.ymatou.iapi.productmanager.testcase;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.BatchSetProductOffSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_BatchSetProductOffSale {
	private static BatchSetProductOffSaleCall batchSetProductOffSaleCall = new BatchSetProductOffSaleCall();
	
	
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量下架商品");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchSetProductOffSaleCall.getOpurl());
	}
	
	

	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_001() {
		Logger.start(true,"下架多个商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();	
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			
			checkOffSaleTime(result,Arrays.asList(productId1,productId2));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_002() {
		Logger.start(true,"下架多个商品,包含正在进行直播商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getLiveProduct(sellerId, sellerName).getProductId();
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkOffSaleTime(result,MapUtil.hashMap(productId1, false, productId2, true));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_003() {
		Logger.start(true,"下架多个商品,包含将来直播商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getFutureLiveProduct(sellerId,sellerName).getProductId();
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkOffSaleTime(result,MapUtil.hashMap(productId1, false, productId2, true));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_004() {
		Logger.start(true,"下架多个商品,包含活动商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getActivityProduct(sellerId, sellerName).getProductId();
			
			ymtProductActivityStockWapper.updateStartTimeByProductId(productId2,  "2016-09-28 17:27:39.000");
			
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId2).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId2).get(0).getValidEnd();
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkOffSaleTime(result,Arrays.asList(productId1));
			
			
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId2).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId2).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "活动商品不能下架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "活动商品不能下架");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_005() {
		Logger.start(true,"下架不属于该买手商品");
		try {
			String productId =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidEnd();
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId1,Arrays.asList(productId));
			
			Logger.verifyEquals(201, result.getInt("Code"), "下架不属于该买手商品 应该失败");
			
			
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "下架不属于该买手商品 不能下架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "下架不属于该买手商品 不能下架");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_006() {
		Logger.start(true,"下架FBX商品");
		try {
			String productId = SellerProductCallService.addFBXProduct(MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
			
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId,Arrays.asList(productId));
			
			checkOffSaleTime(result,Arrays.asList(productId));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOffSale_007() {
		Logger.start(true,"删除商品 无法下架");
		try {
			String productId =  ProductFactory.getOnlyProduct().getProductId();
			
			SellerProductCallService.batchDeleteProduct(sellerId, Arrays.asList(productId));
			
			JSONObject result = batchSetProductOffSaleCall.call(sellerId,Arrays.asList(productId));
			
			Logger.verifyEquals(201, result.getInt("Code"), "返回code应为201");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	public static void checkOffSaleTime(JSONObject ret,List<String> productIdList) throws Exception{
		Map<String,Boolean> productIdAndIsInLive = MapUtil.hashMap();
		productIdList.forEach(x->productIdAndIsInLive.put(x, false));
		checkOffSaleTime(ret,productIdAndIsInLive);
	}
		
		
	
	@SuppressWarnings("rawtypes")
	public static void checkOffSaleTime(JSONObject ret, Map<String,Boolean> productIdAndIsInLive) throws Exception{
		
		Logger.verifyEquals(200, ret.getInt("Code"), "返回code应为200");
		JSONArray actArray = ret.getJSONObject("Data").getJSONArray("offSaledProductIds");
		AssertService.checkArray(actArray, new JSONArray( productIdAndIsInLive.keySet()),"批量下架商品结果个数比对");
		
		
		Date nowDate = YMTDateUtil.getDateBeforeOrNextSecond(600);
		productIdAndIsInLive.forEach((productId,isInLive)->{
			try {
			//SQL YmtProducts
				List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
				Date svalidStartbysql = (Date)productIDsqllist.get(0).get("validStart")	;
				Date svalidEndbysql = (Date)productIDsqllist.get(0).get("validEnd");	
				Logger.verifyEquals(true, svalidStartbysql.before(nowDate), "开始时间早于当前时间");
				Logger.verifyEquals(true, svalidEndbysql.before(nowDate), "结束时间早于当前时间");
				
				
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
				
				if (isInLive){
					List<Map> liveProductMapList = ymtProductsInLiveWapper.selectInLiveByProductId(productId);
					liveProductMapList.forEach(x->{
						int liveId = Integer.parseInt(x.get("iActivityId").toString());
						
						Logger.verifyEquals(-1, Integer.parseInt(x.get("iAction").toString()), "下架商品，iaction应为-1 liveId:"+ liveId
									+", productId:" + x.get("sProductId").toString());
						try {
							//默认下架后，直播中没有有效商品
							Map liveMap = ymtLiveWapper.selectforMapByLiveId(liveId).get(0);
							Logger.verifyEquals(0, Integer.parseInt(liveMap.get("ProductCount").toString()), "下架后, productcount应为0,liveId:"+ liveId);
							Logger.verifyEquals(0, Integer.parseInt(liveMap.get("PutawayCount").toString()), "下架后, PutawayCount应为0,liveId:"+ liveId);
							
							NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId,false);
						} catch (Exception e) {
							e.printStackTrace();
						}
					});
				}	
			} catch (Exception e) {
				e.printStackTrace();
			}
				
		});
	}
	
}
